import { _colors } from "../color/index.js"
import { plainMatrix } from "../math/index.js"
import { spiralPoints } from "../math/points/spiralPoints.js"
import { pointsPath } from "./path.js"
import { _radius, _vertex } from "./polygon.js"
import { _g } from './svg.js'

// 螺旋线
export const _spiral = (options, props) => {
    let {
        r = 50,
        n = 36,
        o = [800 / 2, 600 / 2],
        a = 0,
        vertex,
        radius,
        m = 5,
        color
    } = options
    let matrix = spiralPoints({
        o, r, n, m, a
    })
    let points = plainMatrix(matrix)
    let children = []


    // 节点
    if (vertex) {
        let colors = _colors(color, matrix.length, 0.5)
        matrix.forEach((ps, i) => {
            children.push(..._vertex(ps, {
                props: {
                    fill: colors[i],
                    stroke: 'none'
                },
                ...vertex
            }, props))
        })
    }

    if (radius) {
        children.push(_radius(o, points, radius, props))
    }


    children[children.length] = pointsPath({
        points,
        closed: false,
        broken: false
    }, {
        id: 'spiral',
        fill: 'none',
        stroke: 'black',
    })

    return children.length ? _g(children, {
        id: 'spiral',
        fill: 'none',
        stroke: 'black',
        ...props,
    }) : null
}

export const _twoSpiral = (options, props) => {
    return [0, 180].map(t => _spiral({
        ...options,
        a: options.a + t
    }, props))
}
